package corp.edu.amadeus.utils.file;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;

public class ResourceFinder {

	private Logger log = Logger.getLogger(ResourceFinder.class);

    public ResourceFinder() {

    }


    public String getResourceAsFileSystemPath(String basePackage,
    		String resourceName) {
        return this.getURLToFile(basePackage,resourceName);
    }

    public URL getURL(String resource_package_path) {
    	Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        URL url =  classLoader.getResource(resource_package_path);
        return url;
    }

    public InputStream getResourceAsInputStream(String resourceName) {
        return this.locateResourceAsInputStream(resourceName);
    }

    public InputStream getResourceAsInputStream(String basePackage,
    		String resourceName) {
        return this.locateResourceAsInputStream(basePackage,
        		resourceName);
    }

    public String getPackageAbsPath(String packag) {
        Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        URL url =  classLoader.getResource(packag);
        if(url!=null) {
            return url.getPath();
        }
        return packag;
    }

    public List<String> getFQDNFiles(String base_package_id)
    	throws Exception {
    	if(!base_package_id.endsWith("/")) {
    		base_package_id += "/";
    	}
    	List<String> fqdn_files = new ArrayList<String>();
    	Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        URL url = classLoader.getResource(base_package_id);
        log.debug("Path: " + url.getContent());
        File base_package = new File(url.getPath());
        String file_url = "";
        for(File file : base_package.listFiles()) {
        	if(file.isFile()) {
        		file_url = base_package_id + file.getName();
        		log.debug(file_url);
            	fqdn_files.add(file_url);
        	}
        }
    	return fqdn_files;
    }

    private String getURLToFile(String Base_Folder,
    		String resourceName) {
        Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        URL url =  classLoader.getResource(Base_Folder+resourceName);
        if(url!=null) {
            return url.getPath();
        }
        return Base_Folder+resourceName;
    }

    private InputStream locateResourceAsInputStream(String resourceName) {
        Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        InputStream inputStream =
           classLoader.getResourceAsStream(resourceName);
        return inputStream;
    }

    private InputStream locateResourceAsInputStream(String Base_Folder,
    		String resourceName) {
        Class listenerClass = ResourceFinder.class;
        ClassLoader classLoader = listenerClass.getClassLoader();
        InputStream inputStream =
           classLoader.getResourceAsStream(Base_Folder + resourceName);
        return inputStream;
    }

}